using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Principal;

public partial class Login : System.Web.UI.Page
{
    private string username1, password1;
    protected void Page_Load(object sender, EventArgs e)
    {
        // get references to the button, checkbox and textboxes
        TextBox usernameTextBox = (TextBox)Login1.FindControl("UserName");
        TextBox passwordTextBox = (TextBox)Login1.FindControl("Password");
        CheckBox persistCheckBox = (CheckBox)Login1.FindControl("RememberMe");
        Button loginButton = (Button)Login1.FindControl("LoginButton");
        // tie the two textboxes and the checkbox to the button
        Utilities.TieButton(this.Page, usernameTextBox, loginButton);
        Utilities.TieButton(this.Page, passwordTextBox, loginButton);
        Utilities.TieButton(this.Page, persistCheckBox, loginButton);

        username1 = usernameTextBox.Text;
        password1 = passwordTextBox.Text;
        // set the page title
        this.Title = HunterShopConfiguration.SiteName + " : Zaloguj";
        // set focus on the username textbox when the page loads
        usernameTextBox.Focus();
    }
       

    private string GetRoles(string username, string password)
    {
        // Lookup code omitted for clarity
        // This code would typically look up the role list from a database table.
        // If the user was being authenticated against Active Directory, the
        // Security groups and/or distribution lists that the user belongs to may be 
        // used instead

        // This GetRoles method returns a pipe delimited string containing roles
        // rather than returning an array, because the string format is convenient 
        // for storing in the authentication ticket / cookie, as user data
        return "Administrators|Customers";
    }


    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
            string roles = GetRoles(username1, password1);


            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, username1, DateTime.Now,
            DateTime.Now.AddMinutes(60), false, roles);

            string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

            HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

            //FormsAuthentication.SetAuthCookie(usernameTextBox.Text, false);
            Response.Cookies.Add(authCookie);

            if (Roles.IsUserInRole(username1, "Administrators"))
            {
                Response.Redirect(Request.ApplicationPath + "/Admin/CatalogAdmin.aspx");
            }
            if (Roles.IsUserInRole(username1, "Customers"))
            {
                Response.Redirect(Request.ApplicationPath + "/CustomerPage.aspx");
            }

    }
}
